package com.itheima.controller;

import com.itheima.domain.Address;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author 王加鑫
 * @Date 2021/07/25 22:02
 * @Since JDK 1.8
 */
@Controller
/*@SessionAttributes({"gender","age"})*/
public class ViewResponseController {

    @RequestMapping("/view01")
    public String view01(HttpServletRequest request){
        request.setAttribute("itheima","itheima");
        return "forward:success.jsp";
    }

    @RequestMapping(value = "/view02")
    public String view02(Model model){
        model.addAttribute("itheima","itheima");
        return "redirect:success.jsp";
    }

    @RequestMapping("/view03")
    public ModelAndView view03(ModelAndView modelAndView){
        modelAndView.addObject("name","itheima");
        modelAndView.setViewName("redirect:success.jsp");
        return modelAndView;
    }

    @RequestMapping("/view04")
    @ResponseBody
    public Address view04(){
        Address address = new Address("安徽","安庆");
        return address;
    }

    @RequestMapping("/view05")
    public String view05(HttpServletRequest req, HttpServletResponse resp, HttpSession session){
        System.out.println("req = " + req);
        System.out.println("resp = " + resp);
        System.out.println("session = " + session);
        return "success.jsp";
    }

    @RequestMapping("/view06")
    public String view06(@RequestHeader("Accept-Language") String head){
        System.out.println(head);
        return "success.jsp";
    }

    @RequestMapping("/view07")
    public String view07(@CookieValue("JSESSIONID") String cookie){
        System.out.println(cookie);
        return "success.jsp";
    }


    @RequestMapping("/sessionData")
    public String sessionData(HttpSession session){
        session.setAttribute("name","张三");
        session.setAttribute("gender","男");
        session.setAttribute("age",23);
        return "success.jsp";
    }

    @RequestMapping("/getSession")
    public String getSession(@SessionAttribute("name") String name){
        System.out.println("name = " + name);
        return "success.jsp";
    }

    @RequestMapping("/getSession2")
    public String getSession2(@SessionAttribute("gender")String gender,@SessionAttribute("age")Integer age){
        System.out.println("gender = " + gender);
        System.out.println("age = " + age);
        return "success.jsp";
    }






}
